package pool;

import static org.junit.Assert.*;

import gameObject.Bullet;
import gameObject.GameObject;

import java.awt.Point;
import java.util.Iterator;

import org.junit.Before;
import org.junit.Test;

import pool.BulletPool;
import pool.GameObjectPool;

public class testGameObjectPool
{


	private GameObjectPool<GameObject> emptyPool;
	private Iterator<GameObject> emptyIterator;
	private BulletPool initializedBulletPool;
	private Iterator<Bullet> bulletIterator;
	private BulletPool initializedActiveBulletPool;
	private Iterator<Bullet> activeBulletIterator;
	@Before
	public void setup()
	{
		emptyPool = new GameObjectPool<GameObject>();
		emptyIterator = emptyPool.iterator();
		initializedBulletPool = new BulletPool();
		bulletIterator = initializedBulletPool.iterator();
		initializedActiveBulletPool = new BulletPool();
		for(int i = 0; i < BulletPool.MAX_BULLETS; i++)
		{
			initializedActiveBulletPool.addInactiveBullet();
		}
		activeBulletIterator = initializedActiveBulletPool.iterator();
		verifySetup();
	}
	
	@Test
    public void verifySetup() 
	{
		assertTrue(emptyPool.size() == 0);
		assertTrue(initializedBulletPool.size() == BulletPool.MAX_BULLETS);
		assertTrue(initializedActiveBulletPool.size() == BulletPool.MAX_BULLETS);
	}

//	@After
//	public void after()
//	{
//		
//	}
//	
	
	//************************************************************
	//  bullet pool filled with inactive bullets
	//************************************************************
	@Test
	public void 
	bulletIterator_AfterConstructorCall_VerifyIteratorWorks()
	{
		assertFalse(bulletIterator.hasNext());			
	}
	
	@Test (expected = IllegalStateException.class)
	public void
	bulletIterator_AfterConstructorCall_ThrowsExceptionOnNext()
	{
		bulletIterator.next();
	}
	
	@Test (expected = IllegalStateException.class)
	public void
	bulletIterator_AfterConstructorCall_ThrowsExceptionOnRemove()
	{
		bulletIterator.remove();
	}
	
	//************************************************************
	// bullet pool filled with active bullets
	//************************************************************
	
	@Test
	public void 
	activeBulletIterator_AfterConstructorCall_VerifyIteratorWorksAndNoExceptions()
	{
		assertTrue(activeBulletIterator.hasNext());	
		activeBulletIterator.next();
		activeBulletIterator.remove();		
	}

	
	@Test (expected = IllegalStateException.class)
	public void
	activeBulletIterator_IteratedOverAllBullets_ThrowsExceptionOnNext()
	{
		for(int i = 0 ; i < BulletPool.MAX_BULLETS; i++)
		{
			System.out.println("Iterating over all bullets - throwsExceptionOnNext() : index =  " + i);
			activeBulletIterator.next();
		}
		System.out.println("Iterated over all bullets. Expect exception.");
		activeBulletIterator.next();
	}
	
	@Test 
	public void
	emptyIterator_AfterConstructorCall_HasNextReturnsFalse()
	{
		assertFalse(emptyIterator.hasNext());
	}
	
	@Test (expected = IllegalStateException.class)
	public void
	emptyIterator_AfterConstructorCall_ThrowsExceptionOnNext()
	{
		emptyIterator.next();
	}
	
	@Test (expected = IllegalStateException.class)
	public void
	emptyIterator_AfterConstructorCall_ThrowsExceptionOnRemove()
	{
		emptyIterator.remove();
	}
}
